/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.bean.PurchaseDetailBean;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class PurchaseCountExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"PurchaseCount.xls\"");
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle boldStyle = workbook.createCellStyle();
		boldStyle.setFont(boldFont);
		boldStyle.setBorderBottom(1);
		boldStyle.setBorderTop(1);
		boldStyle.setBorderLeft(1);
		boldStyle.setBorderRight(1);
		HSSFCellStyle norStyle = workbook.createCellStyle();
		norStyle.setBorderBottom(1);
		norStyle.setBorderTop(1);
		norStyle.setBorderLeft(1);
		norStyle.setBorderRight(1);
		HSSFSheet sheet = workbook.createSheet("list");
		String title = "采购汇总表";
		String searchTime = (String) model.get("searchTime");
		if (searchTime != null && !searchTime.equals("")) {
			title = title + "（" + searchTime + "）";
		}

		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		byte colNum = 8;

		int list;
		for (list = 1; list < colNum; ++list) {
			r.createCell((short) list);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString(title));
		sheet.addMergedRegion(new Region(0, 0, 0, (short) (colNum - 1)));
		r = sheet.createRow(1);
		c = r.createCell(0);

		for (list = 1; list < colNum; ++list) {
			r.createCell((short) list);
		}

		c.setCellValue(new HSSFRichTextString("金额合计："
				+ model.get("costTotal").toString()));
		sheet.addMergedRegion(new Region(1, 0, 1, (short) (colNum - 1)));
		r = sheet.createRow(2);
		c = r.createCell(0);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("供应商"));
		c = r.createCell(1);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("进/退"));
		c = r.createCell(2);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("产品编号"));
		c = r.createCell(3);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("品名规格"));
		c = r.createCell(4);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(5);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(6);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单价"));
		c = r.createCell(7);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		List arg19 = (List) model.get("list");

		for (int os = 0; os < arg19.size(); ++os) {
			PurchaseDetailBean b = (PurchaseDetailBean) arg19.get(os);
			String billType = "进";
			if (b.getBillType().equals("BACK")) {
				billType = "退";
			}

			r = sheet.createRow(os + 3);
			c = r.createCell(0);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getProviderName()));
			c = r.createCell(1);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(billType));
			c = r.createCell(2);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getProductCode()));
			c = r.createCell(3);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getProductName()));
			c = r.createCell(4);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getProductUnit()));
			if (b.getBillType().equals("BACK")) {
				c = r.createCell(5);
				c.setCellStyle(norStyle);
				c.setCellValue(0.0D - b.getAmount().doubleValue());
			} else {
				c = r.createCell(5);
				c.setCellStyle(norStyle);
				c.setCellValue(b.getAmount().doubleValue());
			}

			c = r.createCell(6);
			c.setCellStyle(norStyle);
			c.setCellValue(b.getUnitPrice().doubleValue());
			if (b.getBillType().equals("BACK")) {
				c = r.createCell(7);
				c.setCellStyle(norStyle);
				c.setCellValue(0.0D - b.getCost().doubleValue());
			} else {
				c = r.createCell(7);
				c.setCellStyle(norStyle);
				c.setCellValue(b.getCost().doubleValue());
			}
		}

		ServletOutputStream arg20 = response.getOutputStream();
		workbook.write(arg20);
		arg20.flush();
		arg20.close();
	}
}